package com.xin.wx;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;

import javax.lang.model.element.Modifier;

/**
 * 生成微信支付的类
 */
public class WxPayOptUtils {

    /*IWXAPI iwxapi;*/
    public FieldSpec getIWXAPI() {
        ClassName type = ClassName.get("com.tencent.mm.opensdk.openapi", "IWXAPI");
        FieldSpec.Builder builder = FieldSpec.builder(type, "iwxapi")
                .addModifiers(Modifier.PRIVATE);
        return builder.build();
    }

    /*  @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          iwxapi = WXAPIFactory.createWXAPI(this, Constant.WX_AppID);
          iwxapi.handleIntent(getIntent(), this);
      }*/
    public MethodSpec getOnCreate() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onCreate")
                .addModifiers(Modifier.PROTECTED)
                .addParameter(ClassName.get("android.os", "Bundle"), "savedInstanceState")
                .addStatement("super.onCreate(savedInstanceState)")
                .addStatement("iwxapi = $T.createWXAPI(this, $T.WX_AppID)",
                        ClassName.get("com.tencent.mm.opensdk.openapi", "WXAPIFactory"),
                        ClassName.get("com.xin.libandroidpaysocial.share", "Constant"))
                .addStatement("iwxapi.handleIntent(getIntent(), this)")
                .addAnnotation(Override.class);

        return builder.build();
    }

    /*@Override
    public void onReq(BaseReq baseReq) {

    }*/
    public MethodSpec getOnReq() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onReq")
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseReq"), "resp")
                .addAnnotation(Override.class);
        return builder.build();
    }


    public MethodSpec getOnResp() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onResp")
                .addParameter(ClassName.get("com.tencent.mm.opensdk.modelbase", "BaseResp"), "resp")
                .addAnnotation(Override.class)
                .addModifiers(Modifier.PUBLIC)
                .beginControlFlow(" if (resp.getType() == $T.COMMAND_PAY_BY_WX) ", ClassName.get("com.tencent.mm.opensdk.constants", "ConstantsAPI"))
                .addStatement("$T payResult = new PayEventBusData(1)", ClassName.get("com.xin.libandroidpaysocial.pay", "PayEventBusData"))
                .beginControlFlow("if (resp.errCode == BaseResp.ErrCode.ERR_OK)")
                .addStatement("payResult.setState(0)")
                .addStatement("payResult.setMsg($T.PaySuccess)", ClassName.get("com.xin.libandroidpaysocial.pay", "Constants"))
                .nextControlFlow("else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) ")
                .addStatement("payResult.setMsg($T.PayCacal)", ClassName.get("com.xin.libandroidpaysocial.pay", "Constants"))
                .nextControlFlow(" else if (resp.errCode == BaseResp.ErrCode.ERR_COMM) ")
                .addStatement("payResult.setMsg($T.PayError)", ClassName.get("com.xin.libandroidpaysocial.pay", "Constants"))
                .endControlFlow()
                .addStatement("$T.getDefault().post(payResult)", ClassName.get("org.greenrobot.eventbus", "EventBus"))
                .addStatement("finish()")
                .endControlFlow();
        return builder.build();
    }


    /*  @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }*/
    public MethodSpec getOnNewIntent() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("onNewIntent")
                .addModifiers(Modifier.PROTECTED)
                .addParameter(ClassName.get("android.content", "Intent"), "intent")
                .addStatement("super.onNewIntent(intent)")
                .addStatement("setIntent(intent)")
                .addStatement("iwxapi.handleIntent(intent, this)");
        return builder.build();
    }

    /* private void log(String errStr) {
        Log.e(getClass().getSimpleName(), errStr);
    }*/
    public MethodSpec getLog() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("log")
                .addParameter(String.class, "str")
                .addStatement("$T.e(getClass().getSimpleName(), str)", ClassName.get("android.util", "Log"));
        return builder.build();
    }

    /*private void showToast(String str) {
       Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }*/
    public MethodSpec getShowToast() {
        MethodSpec.Builder builder = MethodSpec.methodBuilder("showToast")
                .addParameter(String.class, "str")
                .addStatement("$T.makeText(this, str, Toast.LENGTH_SHORT).show()", ClassName.get("android.widget", "Toast"));
        return builder.build();
    }

}
